package com.yy.permissions_general.module.system.mail.controller;

import com.yy.permissions_general.common.aopSubmit.aop.NoRepeatSubmit;
import com.yy.permissions_general.module.system.aopLog.aop.MyLog;
import com.yy.permissions_general.module.system.mail.entity.Mail;
import com.yy.permissions_general.module.system.mail.service.MailService;
import com.yy.permissions_general.utils.ResultUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;


/**
 * <p>
 * 邮箱前端控制器
 * </p>
 *
 * @author YY
 * @since 2022-02-10
 */
@RestController
@RequestMapping("/mail")
public class MailController {

    @Resource
    private MailService mailService;

    /**
    * 查询列表
    * @param mail
    * @param limit
    * @param page
    * @return
    */
    @MyLog("查询列表")
    @NoRepeatSubmit(key="mail.list")
    @RequestMapping("/list")
    public ResultUtil list(Mail mail,@RequestParam(value = "limit",defaultValue = "999999")int limit, @RequestParam(value = "page",defaultValue = "0")int page) {
      return ResultUtil.buildPagination(mailService.selectPageList(mail,limit,page));
    }

    /**
    * 新增数据
    * @param mail
    * @return
    */
    @MyLog("新增数据")
    @NoRepeatSubmit(key="mail.addMail")
    @PreAuthorize("hasPermission('/mail/addMail','mail:addMail')")
    @RequestMapping("/addMail")
    public ResultUtil addMail(Mail mail) {
        mailService.addMail(mail);
        return ResultUtil.success();
    }

    /**
    * 查看详情
    * @param id
    * @return
    */
    @MyLog("查看详情")
    @RequestMapping("/detailMail")
    public ResultUtil detailMail(String id) {
        Mail mail = mailService.getDetailsById(id);
        return ResultUtil.success(mail);
    }

    /**
    * 根据ID删除记录
    * @param id
    * @return
    */
    @MyLog("删除")
    @NoRepeatSubmit(key="mail.delete")
    @PreAuthorize("hasPermission('/mail/delete','mail:delete')")
    @RequestMapping("/delete")
    public ResultUtil delete(String id) {
        mailService.deleteById(id);
        return ResultUtil.success();
    }

    /**
    * 批量删除数据
    * @param ids ID集合
    * @return
    */
    @MyLog("批量删除")
    @NoRepeatSubmit(key="mail.deleteBatch")
    @PreAuthorize("hasPermission('/mail/deleteBatch','mail:deleteBatch')")
    @RequestMapping("/deleteBatch")
    public ResultUtil deleteBatch(@RequestParam(value = "ids[]",required = true) List<Long> ids) {
        mailService.deleteBatch(ids);
        return ResultUtil.success();
    }

}
